<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户端能访问的资源id集合，注册客户端时，根据实际需要可选择资源id，也可以根据不同的额注册流程，赋予对应的额资源id" prop="clientId">
        <el-input
          v-model="queryParams.clientId"
          placeholder="请输入客户端能访问的资源id集合，注册客户端时，根据实际需要可选择资源id，也可以根据不同的额注册流程，赋予对应的额资源id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册填写或者服务端自动生成，实际应用也有叫app_secret, 必须要有前缀代表加密方式" prop="clientSecret">
        <el-input
          v-model="queryParams.clientSecret"
          placeholder="请输入注册填写或者服务端自动生成，实际应用也有叫app_secret, 必须要有前缀代表加密方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="指定client的权限范围，比如读写权限，比如移动端还是web端权限" prop="scope">
        <el-input
          v-model="queryParams.scope"
          placeholder="请输入指定client的权限范围，比如读写权限，比如移动端还是web端权限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="可选值 授权码模式:authorization_code,密码模式:password,刷新token: refresh_token, 隐式模式: implicit: 客户端模式: client_credentials。支持多个用逗号分隔

作者：谢海凡
链接：https://www.jianshu.com/p/c1c6c966c3a7
来源：简书
著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。" prop="grantTypes">
        <el-select v-model="queryParams.grantTypes" placeholder="请选择可选值 授权码模式:authorization_code,密码模式:password,刷新token: refresh_token, 隐式模式: implicit: 客户端模式: client_credentials。支持多个用逗号分隔

作者：谢海凡
链接：https://www.jianshu.com/p/c1c6c966c3a7
来源：简书
著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。" clearable>
          <el-option
            v-for="dict in dict.type.authorization_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户端重定向uri，authorization_code和implicit需要该值进行校验，注册时填写，" prop="redirectUri">
        <el-input
          v-model="queryParams.redirectUri"
          placeholder="请输入客户端重定向uri，authorization_code和implicit需要该值进行校验，注册时填写，"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设置access_token的有效时间(秒),默认(606012,12小时)" prop="accessValidity">
        <el-input
          v-model="queryParams.accessValidity"
          placeholder="请输入设置access_token的有效时间(秒),默认(606012,12小时)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设置refresh_token有效期(秒)，默认(606024*30, 30填)" prop="refreshValidity">
        <el-input
          v-model="queryParams.refreshValidity"
          placeholder="请输入设置refresh_token有效期(秒)，默认(606024*30, 30填)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.ext_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['oauth:client:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['oauth:client:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['oauth:client:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['oauth:client:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="clientList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户端能访问的资源id集合，注册客户端时，根据实际需要可选择资源id，也可以根据不同的额注册流程，赋予对应的额资源id" align="center" prop="clientId" />
      <el-table-column label="注册填写或者服务端自动生成，实际应用也有叫app_secret, 必须要有前缀代表加密方式" align="center" prop="clientSecret" />
      <el-table-column label="指定client的权限范围，比如读写权限，比如移动端还是web端权限" align="center" prop="scope" />
      <el-table-column label="可选值 授权码模式:authorization_code,密码模式:password,刷新token: refresh_token, 隐式模式: implicit: 客户端模式: client_credentials。支持多个用逗号分隔

作者：谢海凡
链接：https://www.jianshu.com/p/c1c6c966c3a7
来源：简书
著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。" align="center" prop="grantTypes">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.authorization_type" :value="scope.row.grantTypes"/>
        </template>
      </el-table-column>
      <el-table-column label="客户端重定向uri，authorization_code和implicit需要该值进行校验，注册时填写，" align="center" prop="redirectUri" />
      <el-table-column label="设置access_token的有效时间(秒),默认(606012,12小时)" align="center" prop="accessValidity" />
      <el-table-column label="设置refresh_token有效期(秒)，默认(606024*30, 30填)" align="center" prop="refreshValidity" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ext_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['oauth:client:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['oauth:client:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户端对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="客户端ID" prop="clientId" v-if="form.id">
          <el-input v-model="form.clientId" placeholder="客户端ID" />
        </el-form-item>
        <el-form-item label="客户端密钥" prop="clientSecret" v-if="form.id">
          <el-input v-model="form.clientSecret" placeholder="客户端密钥" />
        </el-form-item>
        <el-form-item label="客户端名称" prop="clientName">
          <el-input v-model="form.clientName" placeholder="客户端名称" />
        </el-form-item>
        <el-form-item label="权限范围" prop="scope">
          <el-input v-model="form.scope" placeholder="比如读read写write权限，或者比如移动端app还是web端权限" />
        </el-form-item>
        <el-form-item label="授权模式" prop="grantTypes">
          <el-select v-model="form.grantTypes" placeholder="请选择授权模式">
            <el-option
              v-for="dict in dict.type.authorization_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="重定向URI" prop="redirectUri">
          <el-input v-model="form.redirectUri" placeholder="请输入客户端重定向URI" />
        </el-form-item>
        <el-form-item label="访问令牌时效" prop="accessValidity">
          <el-input v-model="form.accessValidity" placeholder="请输入访问令牌时效，单位是分钟" />
        </el-form-item>
        <el-form-item label="刷新令牌时效" prop="refreshValidity">
          <el-input v-model="form.refreshValidity" placeholder="请输入刷新令牌时效，单位是分钟" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.ext_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listClient, getClient, delClient, addClient, updateClient } from "@/api/oauth/client";

export default {
  name: "Client",
  dicts: ['authorization_type', 'ext_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户端表格数据
      clientList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientId: null,
        clientSecret: null,
        scope: null,
        grantTypes: null,
        redirectUri: null,
        accessValidity: null,
        refreshValidity: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        scope: [
          { required: true, message: "权限范围不能为空", trigger: "blur" }
        ],
        grantTypes: [
          { required: true, message: "授权模式不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户端列表 */
    getList() {
      this.loading = true;
      listClient(this.queryParams).then(response => {
        this.clientList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        clientId: null,
        clientSecret: null,
        clientName: null,
        scope: null,
        grantTypes: null,
        redirectUri: null,
        accessValidity: null,
        refreshValidity: null,
        status: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.clientId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户端";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const clientId = row.clientId || this.ids
      getClient(clientId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户端";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.clientId != null) {
            updateClient(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClient(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const clientIds = row.clientId || this.ids;
      this.$modal.confirm('是否确认删除客户端编号为"' + clientIds + '"的数据项？').then(function() {
        return delClient(clientIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oauth/client/export', {
        ...this.queryParams
      }, `client_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
